package com.example.wheresmystuff2;

import java.util.ArrayList;
import java.util.Date;
import java.util.Map;
import java.util.Set;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

/**
 * This class represents the Search activity, that allows us to search a specific keyword to find items
 * @author Team 26
 */
public class Search extends Activity {

	//preliminary variables for Search
	private static Map<String, ArrayList<Object>> filteredItems;
	private ListView mainListView;  
	private ArrayAdapter<String> listAdapter;
	private ArrayList<String> itemList = new ArrayList<String>();
	static Database db;
	private Activity thing = this;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_search);
		
		db=new Database(this);
		db.open();
		
		//Visuals for the Search activity
		final Button search_button = (Button) findViewById(R.id.search_search);
		final Button cancel_button = (Button) findViewById(R.id.search_cancel);
		final EditText search_field = (EditText) findViewById(R.id.search_input);
		
		filteredItems=FilterPage.filterItems(db.getItemTableAsHashMap(),"Any", "Any", new Date(0,1,1));
		
		mainListView = (ListView) findViewById( R.id.searchListView); 
		
		mainListView.setOnItemClickListener( new OnItemClickListener() {

	    	/**
	    	 * On item click listener for list view
	    	 * 
	    	 * @param arg0
	    	 * @param arg2 position in the list
	    	 * @param arg3
	    	 */
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				String item=mainListView.getItemAtPosition(arg2).toString();
				Toast.makeText(getApplicationContext(), "You chose: "+item, Toast.LENGTH_SHORT).show();
				
		    	Intent myIntent = new Intent(Search.this, ItemPage.class);
		    	//pass string "item" as an extra into new activity
		    	myIntent.putExtra("itemName", item);
		    	//start intent
		    	Search.this.startActivity(myIntent);	
			} 	
	    });
		
		//Search Button OnClickListener to make it search
		search_button.setOnClickListener(new OnClickListener(){
			/**
			 * This onClick listener takes in the search_field text and checks the database for the information
			 */
			public void onClick(View v){
				Map<String, ArrayList<Object>> everything = db.getItemTableAsHashMap();
				Set<String> allItems = everything.keySet();
		    	  
				String search = search_field.getText().toString();
				itemList = new ArrayList<String>();
				for(String s : allItems)	{
					if(s.contains(search)|| ((String) everything.get(s).get(0)).contains(search)||
							((String) everything.get(s).get(1)).contains(search)||
							((String) everything.get(s).get(2)).contains(search)||
							((String) everything.get(s).get(3)).contains(search)||
							((String) everything.get(s).get(4)).contains(search)||
							((String) everything.get(s).get(5)).contains(search)||
							((String) everything.get(s).get(6)).contains(search))
					{
						itemList.add(s);
						Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();
						}
				}  
		    	 
				listAdapter = new ArrayAdapter<String>(thing, R.layout.simplerow, itemList);
				mainListView.setAdapter(listAdapter);  
			}
		});
		 
		//Cancel Button OnClickListener
		cancel_button.setOnClickListener(new OnClickListener()
	    {
			/**
			 * This onClick listener cancels the user's search and brings you back to Gallery activity
			 */
			public void onClick(View v){
				//Go back to gallery
		  		Intent myIntent = new Intent(Search.this, Gallery.class);
		  		Search.this.startActivity(myIntent);
		  	}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.search, menu);
		return true;
	}
}
